//
//  JMTextFildDelegateDefault.m
//  NbTest
//
//  Created by admin on 12-3-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JMTextFildDelegateDefault.h"
#import "JMTextField.h"
#import "NumberKeypadDecimalPoint.h"

@implementation JMTextFildDelegateDefault
@synthesize _isActived;

- (id) init
{
    if (self = [super init])
    {
        
    }
    return self;
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
        _isActived = YES;
    return YES;
    
}





- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
        _isActived = YES;
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    JMTextField* editer = (JMTextField*)textField;
    if (editer.type == EMoneyType)
    {
 //       [NumberKeypadDecimalPoint addTheDecimalPointToTextField:textField];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
     JMTextField* editer = (JMTextField*)textField;
    if (editer.type == EMoneyType)
    {
//        [NumberKeypadDecimalPoint removeButtonFromKeyboard];
    }
    
    if (editer.type == EMoneyType && editer.text.length!=0)
    {
        //去掉前面0
        NSString * text = [editer.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
        while (text.length != 0 && [[text substringToIndex:1] isEqualToString:@"0"])
        {
            text = [text substringFromIndex:1];
        }
        
        //开头是小数点前面＋0
        if (text.length != 0)
        {
            NSRange range = [text rangeOfString:@"."]; 
            if (range.length != 0 )
            {
                if (range.location == 0)
                {
                     NSString* strZero = @"0";
                     text = [strZero stringByAppendingString:text];
                }
            }
        }
        
        editer.text  = text;
    }
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    JMTextField* editer = (JMTextField*)textField;
    if (editer.iLimitInputCount != 0) {
        if ( (range.location + string.length) > editer.iLimitInputCount)
            return NO;
    }
    if (editer.type == EMoneyType)
    {
        if ([string isEqualToString:@""])//删除
        {
            return YES;
        }
        
        //低版本控制
        //if ([[[UIDevice currentDevice] systemVersion] floatValue]<4.1)
        //{
            if ([NSString isDigital:string] == NO)
            {
                return NO;
            }
        //}

        
        NSString* text = textField.text;
        
        //输入小数点
        if ([string isEqualToString:@"."])
        {
            NSRange range1 = [text rangeOfString:@"."];
            if (range1.length != 0 )//发现小数点
            {
                return NO;
            }
            else if (text.length>= (range.location+3))//。输入位置在后三位以前
            {
                return NO;
            }
        }
        
        //小数点后只能输入两位
        
        if (text.length != 0)
        {
            NSRange range1 = [text rangeOfString:@"."]; 
            if (range1.length != 0 )//发现小数点
            {
                //小数点后只能输入两位
                if (text.length>= (range1.location+3) )
                {
                    return NO;
                }
            }
        }
    }
    else if (editer.type == ENumberType)
    {
        if ([string isEqualToString:@""])//删除
        {
            return YES;
        }
        
        if ([NSString isNumber:string] == NO)
        {
            return NO;
        }
    }
    
    // return NO to not change text
    return YES;
}
@end
